/**
 * Created by way on 16/9/17.
 */

const $ = require('../lib/kdom');
import cfg from '../config';
import { get, post, getView } from '../util/api';
import { dateStr } from '../util/tool';
import * as ajax from '../lib/kajax';
import { urlParam } from '../lib/kutil';
import Dialog from '../lib/kdialog';
const dialog = new Dialog();

let _view = require('raw-loader!./evaluate.html'); // eslint-disable-line
// let _view = '';

let issueId = null; // 工单的id

export default class {
  path = '/ogs/evaluate$';
  hash = '/ogs/evaluate';
  title = '服务点评';

  constructor(opt) {
    opt = opt || {};
    this.path = opt.path || this.path;
    this.hash = opt.hash || this.hash;
    this.title = opt.title || this.title;
  }

  // 输出视图
  view(cb) {
    if (!_view) {
      // 静态资源浏览器有缓存,增加时标,强制刷新!
      getView(`${cfg.ogs.view.host}${cfg.ogs.view.evaluate}?t=${(+new Date())}`, '', (rs) => {
        _view = rs;
        cb(null, _view);
      });
    } else
      cb(null, _view);
  }

  // 在已经加载的视图上操作
  bind(dv, params) {
    issueId = params.opts.issueId || urlParam('issueId');
    console.log('我是评价', issueId);
    // dialog.loading();
    // // 问题
    // evtIntr(reviewIssue, 'span', (tag) => {
    //   reviewFun(reviewIssue, tag);
    // });

    // // 服务
    // evtIntr(reviewService, 'span', (tag) => {
    //   reviewFun(reviewService, tag);
    // });
    checkIssue(issueId);
    submitReview.onclick = () => {
      submitReviewFun(issueId);
    };
    // backMsgList.onclick = () => {
    //   $.router.go('', '', true);
    // };

  }
}

// 检查工单
function checkIssue(issueId) {
  // {"rc":200,"data":{"_id":"59853b03a45e5e5d3c84f284","status":0,"rating":[]}}
  get(`${cfg.ogs.host}${cfg.ogs.checkIssue}`, 'id=' + issueId, (rs) => {
    console.log('evaluate checkIssue:   ', rs);
    if (rs) {
      let r = JSON.parse(rs);
      let ret = r.data || {};
      console.log('evaluate checkIssue ret:   ', ret);
      if (r.rc == 200 && ret.status == 2 && ret.rating.length > 0) {
        submitReview.style.display = 'none';
        reviewOpts.style.display = 'none';
        reviewMsg1.style.display = 'block';
      } else {
        submitReview.style.display = 'block';
        reviewOpts.style.display = 'block';
        reviewMsg1.style.display = 'none';
      }
    }
    // dialog.close();
  });
}
// 事件委托
// @param id
// @param node,class
function evtIntr(tid, key, cb) {
  tid.onclick = (e) => {
    const ev = e || window.event;
    const target = ev.target || ev.srcElement;
    if (target.className == key) {
      if (cb) cb(target);
    } else if (target.nodeName.toLowerCase() == key) {
      if (cb) cb(target);
    }
  };
}

// review satr
function reviewFun(tid, tag) {
  if (tid && tag) {
    let els = $.qus(`#${tid.id} .review-icon`);
    let score = $.qu(`#${tid.id} .score`);
    let idx = $.attr(tag, 'idx');// 索引
    if (idx) {
      // console.log(els);
      // console.log(idx);
      for (let i = 0; i < els.length; i++) {
        $.removeClass(els[i], 'active');
      }
      for (let i = 0; i < idx; i++) {
        $.addClass(els[i], 'active');
      }

      // score
      $.html(score, Number(idx) * 2);
    }
  }
}

function submitReviewFun(issueId) {
  if (issueId) {
    let rating = [];
    rating.push($.qu('input[name="ratingEv"]:checked').value);
    rating.push($.qu('input[name="ratingSer"]:checked').value);

    var data = {
      "id": issueId,
      "rating": JSON.stringify(rating),
    };
    console.log(rating);
    ajax.post(`${cfg.ogs.host}${cfg.ogs.review}`, data, (err, rs) => {
      // console.log(JSON.parse(rs));
      if (rs) {
        let r = JSON.parse(rs);
        if (r.rc === 200) {
          // alert('非常感谢您对我们的服务的肯定！');
          submitReview.style.display = 'none';
          reviewOpts.style.display = 'none';
          reviewMsg.style.display = 'block';
        } else {
          alert('非常抱歉，由于网络原因没有评价成功，请退出重试！')
        }
      }
    });
  }
}

// function isShow(val) {
//   if (val == 1) {
//     submitReview.style.display = 'none';
//     reviewOpts.style.display = 'none';
//     reviewMsg.style.display = 'block';
//   } else {
//     submitReview.style.display = 'block';
//     reviewOpts.style.display = 'block';
//     reviewMsg.style.display = 'none';
//     reviewMsg1.style.display = 'none';
//   }
// }

/* function submitReviewFun(issueId) {
  if (issueId) {
    let rating = [];
    rating.push($.html($.qu('#reviewIssue .score')));
    rating.push($.html($.qu('#reviewService .score')));
    var data = {
      "id": issueId,
      "rating": JSON.stringify(rating),
    };
    ajax.post(`${cfg.ogs.host}${cfg.ogs.review}`, data, (err, rs) => {
      // console.log(JSON.parse(rs));
      if (rs) {
        let r = JSON.parse(rs);
        if (r.rc === 200) {
          alert('非常感谢您对我们的服务的肯定！');
        } else {
          alert('非常抱歉，由于网络原因没有评价成功，请退出重试！')
        }
      }
    });
  }
} */